import UIManager from "../cfw/ui/UIManager";

export default class SceneManager {

    private static ins: SceneManager;

    static instance(): SceneManager {
        if (!this.ins) {
            this.ins = new SceneManager();
        }
        return this.ins;
    }

    private isLoading: boolean = false;

    private sceneName: string = ''

    init(node: cc.Node) {
        UIManager.instance().init(node)
    }

    getSceneName() {
        return this.sceneName;
    }

    runScene(asset: cc.SceneAsset) {
        let scene = asset.scene;
        let node = scene.getChildByName("Canvas");
        if (node) {
            this.init(node)
        }
        cc.director.runScene(scene)

        // cc.director.runSceneImmediate(scene)
    }

    loadScene(sceneName: string, callback) {
        if (this.isLoading) {
            return;
        }
        this.isLoading = true;
        cc.director.loadScene(sceneName, (error, scene: cc.Scene) => {
            this.isLoading = false;
            if (error) {
                cc.log("enterScene  error  ", error);
                return;
            }

            // self.publish(UIManager.CHANGE_SCENE,sceneName);
            let node = scene.getChildByName("Canvas");
            if (node) {
                this.init(node)
                callback(null, node);
            } else {
                callback("UIManager enterScene error component is null " + sceneName);
            }
        });
    }

    // /**
    // * 进入一个场景时调用，替代引擎的提供的函数。
    // * @param sceneName 场景名称
    // * @param callback 场景加载回调
    // */
    // loadRes(sceneName: string, loader: ResLoader, callback) {
    //     if (this.isLoading) {
    //         cc.log(+sceneName + "  is already load ");
    //         return;
    //     }
    //     cc.warn('enterScene ', sceneName)
    //     this.isLoading = true;
    //     this.sceneName = sceneName;

    //     loader.loadRes(sceneName, ResType.Scene, (err, item: ResItem) => {
    //         if (err) {
    //             cc.error(' SceneManager  loadRes error ', err)
    //             return;
    //         }
    //         cc.warn('--------------------------------------')
    //         item.getRes();
    //         cc.warn("--------------------------------------- ", sceneName);
    //         // this.exitScene();
    //         // cc.director.runScene(item.getRes())
    //         callback(err, item)
    //     });

    // }
}
